#! /usr/bin/perl -w
#
# (C) 2007 - Wenzel P. P. Peppmeyer
# Released under Artistic License
# see http://code.google.com/p/font4canvas/
#

use Getopt::Long;
my $font_name;
$result = GetOptions("name=s" => \$font_name);

$lines = join("", <>);

print "$font_name = [];\n";

my ($path, $num);
while($lines =~ m/d="([^"]+)"\s+id="char(\d+)"/sg){
  ($path, $num) = ($1, $2);
  
  print "char$num = function(ctx){\nctx.beginPath();\n";
  
  $min_x = 1000;
  $min_y = 1000;
  $max_x = 0;
  $max_y = 0;
  
  while( $path =~ m/(M|C|L)\s([^MCL]+)/g ){
    $c = $1;
    $p = $2;
    $c =~ s/M/ctx.moveTo/;
    $c =~ s/L/ctx.lineTo/;
    $c =~ s/C/ctx.bezierCurveTo/;
    $p =~ s/\s/,/g;
    $p =~ s/,$//;
    
    @t = split(',', $p);
    while( $x = shift(@t), $y = shift(@t) ){
      $min_x = $x if ($x < $min_x);
      $max_x = $x if ($x > $max_x);
      $min_y = $y if ($y < $min_y);
      $max_y = $y if ($y > $max_y);
    }
    
    
    print("\t$c($p);", "\n"); 
  }
  print "ctx.closePath();ctx.fill();\n";
  print "};\n";
  print "char$num.width = $max_x; char$num.height = $max_y;\n";
  print $font_name."[$num] = char$num;\n";
}

print<<EOH;
$font_name\[32] = function(){};
$font_name\[32].width = 4.5;
$font_name\[32].height = 11.5;
EOH
